package edu.ycxy.poetry.interceptor;

import lombok.experimental.Accessors;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author @xin
 * @createDate 2022/4/29
 * @Description 定义处理器拦截器
 */
@Accessors(chain = true)
public class PathInterceptor implements HandlerInterceptor {

    /**
     *
     * @param request 请求对象
     * @param response  响应对象
     * @param handler   处理器（url+Controller：映射）
     * @return 如果返回值为true，则表示放行当前的请求，如果返回值为false，则拦截当前的请求
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getSession().getAttribute("email") == null){
            response.sendRedirect("/login.html");
            return false;
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
